/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JERPFilter.java
 *
 * Created on 2010-5-23, 9:56:32
 */

package cn.ekuma.epos.analysis.situation.erp;

import cn.ekuma.epos.analysis.situation.I_SituationFilterPanel;
import cn.ekuma.epos.analysis.situation.JFilterListPanel;
import cn.ekuma.epos.datalogic.I_DataLogicCustomers;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.user.EditorCreator;
import com.openbravo.format.Formats;

import com.openbravo.bean.infobean.CustomerInfo;
import com.openbravo.bean.Location;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductCategory;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.bean.ProductCategoryExt;
import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.pos.panels.JCategoryTreeDialog;
import com.openbravo.pos.panels.JCustomerFinder;
import com.openbravo.pos.panels.JObjectListFinder;
import com.openbravo.pos.panels.JProductFinder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class JERPFilter extends javax.swing.JPanel implements I_SituationFilterPanel{

    JFilterListPanel jOrderTypeFilter;
    JFilterListPanel jWarehouseFilter;
    JFilterListPanel jProdCateoryFilter;
    JFilterListPanel jProductFilter;
    JFilterListPanel jUserFilter;
    JFilterListPanel jCustomerFilter;

    I_DataLogicCustomers dlCustomers;
    I_DataLogicERP  dlSales;

    /** Creates new form JERPFilter */
    public JERPFilter() {
        initComponents();
        jOrderTypeFilter=new JFilterListPanel() {
            @Override
            public ComboBoxValEntry add() {
                List allUser;
                try {
                    allUser = dlSales.list(OrderType.class);
                    JObjectListFinder userList = JObjectListFinder.newJDialog(this);
                    Object tempUser = userList.showUserList(allUser);
                    if(tempUser!=null){
                        return new ComboBoxValEntry(((OrderType)tempUser).getId(),((OrderType)tempUser).getName());
                    }
                } catch (BasicException ex) {
                   new MessageInf(ex).show(this);
                }
               return null;
            }
        };
        jTabbedPane1.addTab(AppLocal.getIntString("label.ordertype"), jOrderTypeFilter);
        jWarehouseFilter=new JFilterListPanel(){
            @Override
            public ComboBoxValEntry add() {
                List allUser;
                try {
                    allUser = dlSales.list(Location.class);
                    JObjectListFinder userList = JObjectListFinder.newJDialog(this);
                    Object tempUser = userList.showUserList(allUser);
                    if(tempUser!=null){
                        return new ComboBoxValEntry(((Location)tempUser).getID(),((Location)tempUser).getName());
                    }
                } catch (BasicException ex) {
                    new MessageInf(ex).show(this);
                }
               return null;
            }
        };
        jTabbedPane1.addTab(AppLocal.getIntString("label.location"), jWarehouseFilter);
        jProdCateoryFilter=new JFilterListPanel(){
            @Override
            public ComboBoxValEntry add() {
                try {
                    List prodCateorys =  dlSales.list(ProductCategoryExt.class);
                    JCategoryTreeDialog dailog = JCategoryTreeDialog.newJDialog(this);
                    dailog.showDialog(prodCateorys, null);
                    if (dailog.isOK()) {
                        ProductCategory select = (ProductCategory) dailog.getSelected();
                        return new ComboBoxValEntry(select.getID(), select.getName());
                    }
                } catch (BasicException ex) {
                    new MessageInf(ex).show(this);
                }
                return null;
            }
        };
        jTabbedPane1.addTab(AppLocal.getIntString("label.prodcategory"), jProdCateoryFilter);
        jProductFilter=new JFilterListPanel(){
            @Override
            public ComboBoxValEntry add() {
                Product findProduct=JProductFinder.showMessage(this, dlSales);
                if(findProduct!=null){
                    return new ComboBoxValEntry(findProduct.getID(),findProduct.getName());
                }
                return null;
            }
        };
        jTabbedPane1.addTab(AppLocal.getIntString("Menu.Products"), jProductFilter);
        jUserFilter=new JFilterListPanel(){
            @Override
            public ComboBoxValEntry add() {
                List allUser;
                try {
                    allUser = dlSales.listPeopleVisible();
                    JObjectListFinder userList = JObjectListFinder.newJDialog(this);
                    Object tempUser = userList.showUserList(allUser);
                    if(tempUser!=null){
                        return new ComboBoxValEntry(((UserInfo)tempUser).getId(),((UserInfo)tempUser).getName());
                    }
                } catch (BasicException ex) {
                   new MessageInf(ex).show(this);
                }
               return null;
            }
        };
        jTabbedPane1.addTab(AppLocal.getIntString("Menu.Users"), jUserFilter);
        jCustomerFilter=new JFilterListPanel(){
            @Override
            public ComboBoxValEntry add() {
               JCustomerFinder customerFinder=JCustomerFinder.getCustomerFinder(this, dlSales);
               customerFinder.setVisible(true);
                CustomerInfo selectCustomer = customerFinder.getSelectedCustomer();
                if(selectCustomer!=null){
                    return new ComboBoxValEntry(selectCustomer.getId(),selectCustomer.getName());
                }
                return null;
            }
        };
        jTabbedPane1.addTab(AppLocal.getIntString("label.customer"), jCustomerFilter);

        jDateType.addItem(new ComboBoxValEntry(Calendar.DATE,AppLocal.getIntString("list.Calendar.DATE")));
        jDateType.addItem(new ComboBoxValEntry(Calendar.WEEK_OF_YEAR,AppLocal.getIntString("list.Calendar.WEEK_OF_YEAR")));
        jDateType.addItem(new ComboBoxValEntry(Calendar.MONTH,AppLocal.getIntString("list.Calendar.MONTH")));
        jDateType.addItem(new ComboBoxValEntry(Calendar.YEAR,AppLocal.getIntString("list.Calendar.YEAR")));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel7 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTxtStartDate = new javax.swing.JTextField();
        btnDateStart = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTxtEndDate = new javax.swing.JTextField();
        btnDateEnd = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jDateType = new javax.swing.JComboBox();

        setLayout(new java.awt.BorderLayout());
        add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        jLabel3.setText(AppLocal.getIntString("Label.StartDate")); // NOI18N

        jTxtStartDate.setPreferredSize(new java.awt.Dimension(200, 25));

        btnDateStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
        btnDateStart.setPreferredSize(new java.awt.Dimension(50, 25));
        btnDateStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDateStartActionPerformed(evt);
            }
        });

        jLabel4.setText(AppLocal.getIntString("Label.EndDate")); // NOI18N

        jTxtEndDate.setPreferredSize(new java.awt.Dimension(200, 25));

        btnDateEnd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
        btnDateEnd.setPreferredSize(new java.awt.Dimension(50, 25));
        btnDateEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDateEndActionPerformed(evt);
            }
        });

        jButton1.setText(AppLocal.getIntString("button.reset")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(14, 14, 14)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jTxtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDateStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jTxtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDateEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDateType, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(54, 54, 54))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel3)
                    .addComponent(jTxtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDateStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4)
                    .addComponent(jTxtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDateEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jDateType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        add(jPanel7, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateStartActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(date));
        }
}//GEN-LAST:event_btnDateStartActionPerformed

    private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateEndActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(date));
        }
}//GEN-LAST:event_btnDateEndActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        jOrderTypeFilter.removeAll();
        jWarehouseFilter.removeAll();
        jProdCateoryFilter.removeAll();
        jProductFilter.removeAll();
        jUserFilter.removeAll();
        jCustomerFilter.removeAll();
        jTxtStartDate.setText(null);
        jTxtEndDate.setText(null);
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDateEnd;
    private javax.swing.JButton btnDateStart;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jDateType;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTxtEndDate;
    private javax.swing.JTextField jTxtStartDate;
    // End of variables declaration//GEN-END:variables


    public Object createValue() throws BasicException {
        Object[] afilter=new Object[17];
        // orderType
        if(jOrderTypeFilter.getSelectedObjs()!=null){
            afilter[0]=QBFCompareEnum.COMP_IN;
            afilter[1]=jOrderTypeFilter.getSelectedObjs();
        }else{
            afilter[0]=QBFCompareEnum.COMP_NONE;
            afilter[1]=null;
        }
         // Date range
        Object startdate = Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
        Object enddate = Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());

        afilter[2] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
        afilter[3] = startdate;
        afilter[4] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
        afilter[5] = enddate;

        // Warehouse
        if(jWarehouseFilter.getSelectedObjs()!=null){
            afilter[6]=QBFCompareEnum.COMP_IN;
            afilter[7]=jWarehouseFilter.getSelectedObjs();
        }else{
            afilter[6]=QBFCompareEnum.COMP_NONE;
            afilter[7]=null;
        }

        // User
        if(jUserFilter.getSelectedObjs()!=null){
            afilter[8]=QBFCompareEnum.COMP_IN;
            afilter[9]=jUserFilter.getSelectedObjs();
        }else{
            afilter[8]=QBFCompareEnum.COMP_NONE;
            afilter[9]=null;
        }

        // Customer
        if(jCustomerFilter.getSelectedObjs()!=null){
            afilter[10]=QBFCompareEnum.COMP_IN;
            afilter[11]=jCustomerFilter.getSelectedObjs();
        }else{
            afilter[10]=QBFCompareEnum.COMP_NONE;
            afilter[11]=null;
        }

       // Product
        if(jProductFilter.getSelectedObjs()!=null){
            afilter[12]=QBFCompareEnum.COMP_IN;
            afilter[13]=jProductFilter.getSelectedObjs();
        }else{
            afilter[12]=QBFCompareEnum.COMP_NONE;
            afilter[13]=null;
        }

        // Product Cateory
        if(jProdCateoryFilter.getSelectedObjs()!=null){
            afilter[14]=QBFCompareEnum.COMP_IN;
            afilter[15]=jProdCateoryFilter.getSelectedObjs();
        }else{
            afilter[14]=QBFCompareEnum.COMP_NONE;
            afilter[15]=null;
        }

        //时间过滤格式
        afilter[16]=((ComboBoxValEntry)jDateType.getSelectedItem()).getKey();

        return afilter;
    }

	@Override
	public void init(I_DataLogicERP dlSales, I_DataLogicCustomers dlCustomers) {
		this.dlCustomers=dlCustomers;
	    this.dlSales=dlSales;
	}


}
